import {
	promisic
} from "./util";
import  config from "@/config/config";
import store from "../store/index"

class Http {
	static async post(url, data = {}) {
		return this.request({
			url,
			data,
			method: "POST"
		})
	}

	static async request({
		url,
		data,
		method = 'GET'
	}) {



		const res = await promisic(uni.request)({
			url: `${config.apiBaseUrl}${url}`,
			data,
			method,
			header: {
				token: store.state.token,
				language: store.state.langKey
			}
		});
		if (res.data.code == 401 || res.statusCode == 401) {
			store.commit('logout');
			uni.reLaunch({
				url: '/pages/index/index'
			})
			// return await Http.request({url, data, method})
		} else {
			return res.data;
		}
	}


	static async upload(filePath, formData = {}) {
		const res = await promisic(uni.uploadFile)({
			url: `${config.apiBaseUrl}ajax/upload`,
			filePath: filePath,
			name: 'file',
			formData,
			header: {
				token: store.state.token,
				language: store.state.langKey
			}
		});

		if (res.data.code == 401) {
			store.commit('logout');
		} else {
			return JSON.parse(res.data);
		}

	}


}

export {
	Http
}